/*
 * Copyright (C) 2013 The i18nCTS Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.i18nCTS;

import java.util.Calendar;

import android.app.ActionBar;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.googlecode.i18nCTS.provider.i18nCTSProvider;
import com.googlecode.i18nCTS.view.CalendarSectionFragment;
import com.googlecode.i18nCTS.view.DateFormatSectionFragment;
import com.googlecode.i18nCTS.view.DateUtilsListViewSectionFragment;
import com.googlecode.i18nCTS.view.NumberSectionFragment;
import com.googlecode.i18nCTS.view.RunAllSectionFragment;
import com.googlecode.i18nCTS.view.StringSectionFragment;

public class MainActivity extends FragmentActivity implements
		ActionBar.OnNavigationListener {

	private static final String TAG = "I18nCTS_" + MainActivity.class.getSimpleName();
	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
	Context mContext = null;
	
	private void checkDeviceSettings() {
	    Calendar c = Calendar.getInstance();
	    int yr = c.get(Calendar.YEAR);
	    if (yr != 2013) {
	    	Toast toast = Toast.makeText(getApplicationContext(), 
	    			"Device Date is not set to 2013.  Please set to 2013", Toast.LENGTH_LONG);
	    	toast.show();
	    }
	}
	 
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.run_all:
	        	i18nCTSProvider file = new i18nCTSProvider(mContext);
	        	if (file.importAllXMLFile()) {
	        		onNavigationItemSelected(100, 0);
	        	}
	        	else {
		        	Toast toast = Toast.makeText(getApplicationContext(), "Error executing \'Run All\'", Toast.LENGTH_LONG);
		        	toast.show();
	        	}	        	
	        	return true;
	        	
	        case R.id.menu_about:
	        	Toast toast = Toast.makeText(getApplicationContext(), "Contributions provided by Motorla Internationalization team", Toast.LENGTH_LONG);
	        	toast.show();
	            return true;
	        	
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mContext = this;
		
		// Set up the action bar to show a dropdown list.
		final ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

		checkDeviceSettings();
		
		// Set up the dropdown list navigation in the action bar.
		actionBar.setListNavigationCallbacks(
		// Specify a SpinnerAdapter to populate the dropdown list.
				new ArrayAdapter<String>(actionBar.getThemedContext(),
						android.R.layout.simple_list_item_1,
						android.R.id.text1, new String[] {
								getString(R.string.api_dateutils),
								getString(R.string.api_dateformat),
								getString(R.string.api_numberformat),
								getString(R.string.api_calendar),
								getString(R.string.api_string), }), this);
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		// Restore the previously serialized current dropdown position.
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(
					savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		// Serialize the current dropdown position.
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
				.getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onNavigationItemSelected(int position, long id) {
		Fragment fragment = null;
		switch (position) {
			case 0: // date and time
				fragment = new DateUtilsListViewSectionFragment();
				break;
			
			case 1: // date format
				fragment = new DateFormatSectionFragment();
				break;

			case 2: // number
				fragment = new NumberSectionFragment();
				break;

			case 3: // calendar
				fragment = new CalendarSectionFragment();
				break;
				
			case 4: // string
				fragment = new StringSectionFragment();
				break;

			default:
				Log.w(TAG, getString(R.string.log_unknown));
			case 100: // run-all
				fragment = new RunAllSectionFragment();
				break;
		}
		
		getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
		return true;
	}

}
